package com.csjbot.weedle_auto.view

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView

import com.csjbot.weedle_auto.R

/**
 * Created by chenqi on 8/7/17.
 */

class CustomTitleBar : LinearLayout {
    /**
     * 标题栏的根布局
     */
    /**
     * 获取标题栏的跟布局
     *
     * @return LinearLayout
     */
    private lateinit var ll: LinearLayout
    /**
     * 标题栏的左边按返回按钮
     */
    /**
     * 获取左边的返回按钮
     *
     * @return Button
     */
    private lateinit var left_button: TextView
    /**
     * 标题栏的右边按保存按钮
     */
    /**
     * 获取右边的保存按钮
     *
     * @return Button
     */
    private lateinit var right_button: TextView
    /**
     * 标题栏的中间的文字
     */
    /**
     * 获取标题栏标题TextView
     *
     * @return TextView
     */
    lateinit var title: TextView
    /**
     * 标题栏的背景颜色
     */
    private var title_background_color: Int = 0
    /**
     * 标题栏的显示的标题文字
     */
    private var title_text: String? = null
    /**
     * 标题栏的显示的标题文字颜色
     */
    private var title_textColor: Int = 0
    /**
     * 标题栏的显示的标题文字大小
     */
    private var title_textSize: Int = 0


    /**
     * 返回按钮的资源图片
     */
    private var left_button_imageId: Int = 0
    /**
     * 返回按钮上显示的文字
     */
    private var left_button_text: String? = null
    /**
     * 返回按钮上显示的文字颜色
     */
    private var left_button_textColor: Int = 0
    /**
     * 返回按钮上显示的文字大小
     */
    private var left_button_textSize: Int = 0
    /**
     * 是否显示返回按钮
     */
    private var show_left_button: Boolean = false


    /**
     * 右边保存按钮的资源图片
     */
    private var right_button_imageId: Int = 0
    /**
     * 右边保存按钮的文字
     */
    private var right_button_text: String? = null
    /**
     * 右边保存按钮的文字颜色
     */
    private var right_button_textColor: Int = 0
    /**
     * 右边保存按钮的文字大小
     */
    private var right_button_textSize: Int = 0
    /**
     * 是否显示右边保存按钮
     */
    private var show_right_button: Boolean = false

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        /**加载布局文件 */
        LayoutInflater.from(context).inflate(R.layout.pub_titlebar, this, true)
        ll = findViewById(R.id.ll) as LinearLayout
        left_button = findViewById(R.id.left_button) as TextView
        right_button = findViewById(R.id.right_button) as TextView
        title = findViewById(R.id.title) as TextView

        /**获取属性值 */
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleBar)
        /**标题相关 */
        title_background_color = typedArray.getColor(R.styleable.CustomTitleBar_title_background, Color.WHITE)
        title_text = typedArray.getString(R.styleable.CustomTitleBar_title_text)
        title_textColor = typedArray.getColor(R.styleable.CustomTitleBar_title_textColor, Color.BLACK)
        title_textSize = typedArray.getColor(R.styleable.CustomTitleBar_title_textSize, 22)
        /**返回按钮相关 */
        left_button_imageId = typedArray.getResourceId(R.styleable.CustomTitleBar_left_button_image, R.mipmap.ic_launcher)
        left_button_text = typedArray.getString(R.styleable.CustomTitleBar_left_button_text)
        left_button_textColor = typedArray.getColor(R.styleable.CustomTitleBar_left_button_textColor, Color.WHITE)
        left_button_textSize = typedArray.getColor(R.styleable.CustomTitleBar_left_button_textSize, 20)
        show_left_button = typedArray.getBoolean(R.styleable.CustomTitleBar_show_left_button, true)
        /**右边保存按钮相关 */
        right_button_imageId = typedArray.getResourceId(R.styleable.CustomTitleBar_right_button_image, 0)
        right_button_text = typedArray.getString(R.styleable.CustomTitleBar_right_button_text)
        right_button_textColor = typedArray.getColor(R.styleable.CustomTitleBar_right_button_textColor, Color.WHITE)
        right_button_textSize = typedArray.getColor(R.styleable.CustomTitleBar_right_button_textSize, 20)
        show_right_button = typedArray.getBoolean(R.styleable.CustomTitleBar_show_right_button, true)
        /**设置值 */

        setTitle_background_color(title_background_color)
        setTitle_text(title_text)
        setTitle_textSize(title_textSize)
        setTitle_textColor(title_textColor)
        setShow_left_button(show_left_button)
        setShow_right_button(show_right_button)
        if (!TextUtils.isEmpty(left_button_text)) {//返回按钮显示为文字
            setLeft_button_text(left_button_text!!)
            setLeft_button_textColor(left_button_textColor)
            setLeft_button_textSize(left_button_textSize)
        } else {
            setLeft_button_imageId(left_button_imageId)
        }

        if (!TextUtils.isEmpty(right_button_text)) {//返回按钮显示为文字
            setRight_button_text(right_button_text!!)
            setRight_button_textColor(right_button_textColor)
            setRight_button_textSize(right_button_textSize)
        } else {
            setRight_button_imageId(right_button_imageId)
        }
        this.left_button.setOnClickListener {
            if (titleOnClickListener != null) {
                titleOnClickListener!!.onLeftClick()
            }
        }
        right_button.setOnClickListener {
            if (titleOnClickListener != null) {
                titleOnClickListener!!.onRightClick()
            }
        }
        this.left_button = left_button
    }

    /**
     * 设置返回按钮的资源图片id
     *
     * @param left_button_imageId 资源图片id
     */
    fun setLeft_button_imageId(left_button_imageId: Int) {
        left_button.setBackgroundResource(left_button_imageId)
    }

    /**
     * 设置返回按钮的文字
     *
     * @param left_button_text
     */
    fun setLeft_button_text(left_button_text: String) {
        left_button.text = left_button_text
    }

    /**
     * 设置返回按钮的文字颜色
     *
     * @param left_button_textColor
     */
    fun setLeft_button_textColor(left_button_textColor: Int) {
        left_button.setTextColor(left_button_textColor)
    }

    /**
     * 设置返回按钮的文字大小
     *
     * @param left_button_textSize
     */
    fun setLeft_button_textSize(left_button_textSize: Int) {
        left_button.textSize = left_button_textSize.toFloat()
    }

    /**
     * 设置是否显示返回按钮
     *
     * @param show_left_button
     */
    fun setShow_left_button(show_left_button: Boolean) {
        left_button.visibility = if (show_left_button) View.VISIBLE else View.INVISIBLE
    }


    /**
     * 设置右边保存按钮的资源图片
     *
     * @param right_button_imageId
     */
    fun setRight_button_imageId(right_button_imageId: Int) {
        right_button.setBackgroundResource(right_button_imageId)
    }

    /**
     * 设置右边的保存按钮的文字
     *
     * @param right_button_text
     */
    fun setRight_button_text(right_button_text: String) {
        right_button.text = right_button_text
    }

    /**
     * 设置右边保存按钮的文字颜色
     *
     * @param right_button_textColor
     */
    fun setRight_button_textColor(right_button_textColor: Int) {
        right_button.setTextColor(right_button_textColor)
    }

    /**
     * 设置右边保存按钮的文字大小
     *
     * @param right_button_textSize
     */
    fun setRight_button_textSize(right_button_textSize: Int) {
        right_button.textSize = right_button_textSize.toFloat()
    }

    /**
     * 设置是显示右边保存按钮
     *
     * @param show_right_button
     */
    fun setShow_right_button(show_right_button: Boolean) {
        right_button.visibility = if (show_right_button) View.VISIBLE else View.INVISIBLE
    }


    /**
     * 设置标题背景的颜色
     *
     * @param title_background_color
     */
    fun setTitle_background_color(title_background_color: Int) {
        ll.setBackgroundColor(title_background_color)
    }

    /**
     * 设置标题的文字
     *
     * @param title_text
     */
    fun setTitle_text(title_text: String?) {
        title.text = title_text
    }

    /**
     * 设置标题的文字颜色
     *
     * @param title_textColor
     */
    fun setTitle_textColor(title_textColor: Int) {
        title.setTextColor(title_textColor)
    }

    /**
     * 设置标题的文字大小
     *
     * @param title_textSize
     */
    fun setTitle_textSize(title_textSize: Int) {
        title.textSize = title_textSize.toFloat()
    }


    private var titleOnClickListener: TitleOnClickListener? = null
    /**
     * 设置标题的点击监听
     *
     * @param titleOnClickListener
     */
    fun setOnTitleClickListener(titleOnClickListener: TitleOnClickListener) {
        this.titleOnClickListener = titleOnClickListener
    }

    /**
     * 监听标题点击接口
     */
    interface TitleOnClickListener {
        /**
         * 返回按钮的点击事件
         */
        fun onLeftClick()

        /**
         * 保存按钮的点击事件
         */
        fun onRightClick()

    }
}
